/**
 * 
 */
package com.wilmar.process.core.services.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.wilmar.process.core.ProcessSystemDeclareKeys;
import com.wilmar.process.core.services.spi.IClassConvertService;
import com.wilmar.process.exception.UnSupportedClassConvertorException;

/**
 *通用数据类型转换器，将字符串数据根据提供的java类型进行转换，目前支持String,Integer,Long,Float,Double,java.
 * util. Data类型转换
 * 
 * @author MaKuBEX E-mail:makubexnet@gmail.com
 * @version 1.0.0 at Aug 4, 2009 1:49:00 PM
 * 
 */
public class CommonClassConvertServiceImpl implements IClassConvertService {

	/**
	 * @see com.wilmar.process.core.services.spi.IClassConvertService#convert(java.lang.String,
	 *      java.lang.Class)
	 */
	public Object convert(String value, @SuppressWarnings("rawtypes") Class clazz) throws ParseException,
			UnSupportedClassConvertorException {
		if (Integer.class == clazz)
			return Integer.parseInt(value);
		else if (Long.class == clazz)
			return Long.parseLong(value);
		else if (Double.class == clazz)
			return Double.parseDouble(value);
		else if (Float.class == clazz)
			return Float.parseFloat(value);
		else if (Date.class == clazz) {
			String format = ProcessSystemDeclareKeys.SYSTEM_LONG_DATE_FORMAT
					+ ProcessSystemDeclareKeys.SPACE
					+ ProcessSystemDeclareKeys.SYSTEM_LONG_TIME_FORMAT;
			if (value.length() != format.length())
				format = ProcessSystemDeclareKeys.SYSTEM_LONG_DATE_FORMAT
						+ ProcessSystemDeclareKeys.SPACE
						+ ProcessSystemDeclareKeys.SYSTEM_SHORT_TIME_FORMAT;
			if (value.length() != format.length())
				format = ProcessSystemDeclareKeys.SYSTEM_LONG_DATE_FORMAT;
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.parse(value);
		} else if (String.class == clazz)
			return value;
		else
			throw new UnSupportedClassConvertorException("Type:" + clazz
					+ " is not supported by this convertor!");
	}

}
